package com.mutual.help.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mutual.help.common.PageResult;
import com.mutual.help.entity.Comment;
import com.mutual.help.entity.Question;
import com.mutual.help.entity.Type;
import com.mutual.help.service.CommentService;
import com.mutual.help.service.QuestionService;
import com.mutual.help.service.TypeService;
import com.mutual.help.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class QuestionController{

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private TypeService typeService;

    //页面跳转
    @RequestMapping("/question/toPage")
    public String toPage(){
        return "question/list";
    }

    //页面带参数跳转
    @RequestMapping("/question/toEditPage")
    public String toEditPage(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        Question question = questionService.getById(Integer.parseInt(id));
        List<Type> allTypeName = typeService.list();
        model.addAttribute("question",question);
        model.addAttribute("allTypeName",allTypeName);
        return "question/edit";
    }

    @RequestMapping("wx/addQuestion")
    @ResponseBody
    public Object addQuestion(@RequestBody Question question){
        if (questionService.save(question))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    @RequestMapping("wx/getAllQuestionByType")
    @ResponseBody
    public Object getAllQuestionByType(HttpServletRequest request){
        String type = request.getParameter("type");
        QueryWrapper<Question> wrapper = new QueryWrapper();
        wrapper.eq("type", Integer.parseInt(type));
        List<Question> list = questionService.list(wrapper);
        return WeChatResponseUtil.ok(list);
    }

    @RequestMapping("wx/getQusByKeyWord")
    @ResponseBody
    public Object getQusByKeyWord(HttpServletRequest request){
        String key = request.getParameter("key");
        QueryWrapper<Question> wrapper = new QueryWrapper();
        wrapper.like("title", key).or().like("content", key);
        List<Question> list = questionService.list(wrapper);
        return WeChatResponseUtil.ok(list);
    }

    @RequestMapping("wx/myPublishQst")
    @ResponseBody
    public Object myPublishQst(HttpServletRequest request){
        String userId = request.getParameter("userId");
        QueryWrapper<Question> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        List<Question> list = questionService.list(wrapper);
        return WeChatResponseUtil.ok(list);
    }

    @RequestMapping("wx/myAnswerQst")
    @ResponseBody
    public Object myAnswerQst(HttpServletRequest request){
        String userId = request.getParameter("userId");
        QueryWrapper<Comment> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        List<Comment> commentList = commentService.list(wrapper);
        List<Integer> ids = new ArrayList<>();
        for (Comment comment: commentList){
            ids.add(comment.getQuestionId());
        }
        List<Integer> listWithoutDuplicates = ids.stream().distinct().collect(Collectors.toList()); //把问题id去重
        Collection<Question> questions = questionService.listByIds(listWithoutDuplicates);
        return WeChatResponseUtil.ok(questions);
    }

    //分页查询
    @RequestMapping("/question/query")
    @ResponseBody
    public PageResult<Question> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Question> allArticle = questionService.getAllQuestion(page, limit);
        Integer count = questionService.countData();
        return new PageResult<>(allArticle, count);
    }

    //更新问题
    @RequestMapping("/question/update")
    @ResponseBody
    public PageResult update(Question type){
        if(questionService.updateById(type)){
            return new PageResult("修改成功",200);
        }
        return new PageResult("修改失败",400);
    }

    @RequestMapping("/question/delete/{id}")
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(questionService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }
}
